#include "event_filters.h"

#include <QEvent>
#include <QTimer>

#include <QAbstractSpinBox>
#include <QLineEdit>

bool SelectAllOnFocusIn::eventFilter( QObject * watched, QEvent * event ) {
    QAbstractSpinBox * asb = qobject_cast<QAbstractSpinBox *>( watched );
    QLineEdit * le = qobject_cast<QLineEdit *>( watched );
    if ( asb )
    {
        if ( event->type() == QEvent::FocusIn )
        { QTimer::singleShot( 10, asb, &QAbstractSpinBox::selectAll ); }
    } else if ( le )
    {
        if ( event->type() == QEvent::FocusIn )
        { QTimer::singleShot( 10, le, &QLineEdit::selectAll ); }
    }
    return QObject::eventFilter( watched, event );
}

bool ConvertFromLocaleNumber::eventFilter( QObject * watched, QEvent * event ) {
    QLineEdit * le = qobject_cast<QLineEdit *>( watched );
    if ( le )
    {
        if ( event->type() == QEvent::FocusIn )
        { le->setText( QString::number( le->locale().toDouble( le->text() ), 'f', 0 ) ); }
    }
    return QObject::eventFilter( watched, event );
}
